package com.goktech.controller;

import com.goktech.entity.*;
import com.goktech.service.*;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @ClassName adminInformController
 * @Description TODO
 * @Author 邱华英
 * @Date 2022/10/10 20:33
 * @Version 1.0
 */

@Controller
@RequestMapping("/admin/inform")
public class adminInformController {

    @Autowired
    private InformService informService;

    @Autowired
    private AdminService adminService;

    @Autowired
    private Admin admin;

    @Autowired
    private InformTypeService informTypeService;

    @Autowired
    private UserService userService;

    @Autowired
    private inform inform1;

    @Autowired
    private User user;

    @Autowired
    private BorrowService borrowService;

    @Autowired
    private borrow borrow1;

    @RequestMapping("/all/")
    public String InformSelectAll(HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.setAttribute("search", "all");
        List<informType> all = informTypeService.findAll();
        session.setAttribute("informType", all);
        return "redirect:add?yeshu=0";
    }

    @RequestMapping("/all/add")
    public String searchAdd(Integer yeshu, HttpServletRequest request) {

        HttpSession session = request.getSession();
        String azhanghao = (String) session.getAttribute("Azhanghao");
        admin.setAzhanghao(azhanghao);
        Admin byZhanghao = adminService.findByZhanghao(admin);

        List<Map<String, Object>> maps = null;
        maps = informService.adminfindByAll(byZhanghao, yeshu * 5, 5);

        if (maps.size() == 0 && yeshu != 0) {
            maps = informService.adminfindByAll(byZhanghao, (yeshu - 1) * 5, 5);
        } else {
            yeshu = yeshu + 1;
        }
        request.setAttribute("yeshu", yeshu);
        request.setAttribute("inform", maps);
        System.out.println("yeshu:" + yeshu);
        return "admin/adminInform";
    }

    @RequestMapping("/all/delete")
    public String searchDelete(Integer yeshu, HttpServletRequest request) {

        HttpSession session = request.getSession();
        String azhanghao = (String) session.getAttribute("Azhanghao");
        admin.setAzhanghao(azhanghao);
        Admin byZhanghao = adminService.findByZhanghao(admin);

        List<Map<String, Object>> maps = null;
        if (yeshu > 1) {
            yeshu = yeshu - 1;
        }
        maps = informService.adminfindByAll(byZhanghao, (yeshu - 1) * 5, 5);
        request.setAttribute("yeshu", yeshu);
        request.setAttribute("inform", maps);
        return "admin/adminInform";
    }

    @RequestMapping("/public/")
    public String publicAll(HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.setAttribute("search", "public");
        return "redirect:add?yeshu=0";
    }

    @RequestMapping("/public/add")
    public String publicAdd(Integer yeshu, HttpServletRequest request) {
        List<Map<String, Object>> maps = null;
        maps = informService.adminPublicAll(yeshu * 5, 5);
        if (maps.size() == 0 && yeshu != 0) {
            maps = informService.adminPublicAll((yeshu - 1) * 5, 5);
        } else {
            yeshu = yeshu + 1;
        }
        request.setAttribute("yeshu", yeshu);
        request.setAttribute("inform", maps);
        return "admin/adminInform";
    }

    @RequestMapping("/public/delete")
    public String publicDelete(Integer yeshu, HttpServletRequest request) {
        List<Map<String, Object>> maps = null;
        if (yeshu > 1) {
            yeshu = yeshu - 1;
        }
        maps = informService.adminPublicAll((yeshu - 1) * 5, 5);
        request.setAttribute("yeshu", yeshu);
        request.setAttribute("inform", maps);
        return "admin/adminInform";
    }

    @RequestMapping("/private/")
    public String InformPrivateAll(HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.setAttribute("search", "private");
        return "redirect:add?yeshu=0";
    }

    @RequestMapping("/private/add")
    public String PrivateAdd(Integer yeshu, HttpServletRequest request) {

        HttpSession session = request.getSession();
        String azhanghao = (String) session.getAttribute("Azhanghao");
        admin.setAzhanghao(azhanghao);
        Admin byZhanghao = adminService.findByZhanghao(admin);

        List<Map<String, Object>> maps = null;
        maps = informService.adminPrivateByAll(byZhanghao, yeshu * 5, 5);
        System.out.println("maps:" + maps);
        if (maps.size() == 0 && yeshu != 0) {
            maps = informService.adminPrivateByAll(byZhanghao, (yeshu - 1) * 5, 5);
        } else {
            yeshu = yeshu + 1;
        }
        request.setAttribute("yeshu", yeshu);
        request.setAttribute("inform", maps);
        System.out.println("yeshu:" + yeshu);
        return "admin/adminInform";
    }

    @RequestMapping("/private/delete")
    public String PrivateDelete(Integer yeshu, HttpServletRequest request) {

        HttpSession session = request.getSession();
        String azhanghao = (String) session.getAttribute("Azhanghao");
        admin.setAzhanghao(azhanghao);
        Admin byZhanghao = adminService.findByZhanghao(admin);

        List<Map<String, Object>> maps = null;
        if (yeshu > 1) {
            yeshu = yeshu - 1;
        }
        maps = informService.adminPrivateByAll(byZhanghao, (yeshu - 1) * 5, 5);
        request.setAttribute("yeshu", yeshu);
        request.setAttribute("inform", maps);
        return "admin/adminInform";
    }

    @PostMapping("/select")
    public String select(@Param("select") String select, @Param("text") String text, HttpServletRequest request) {
        HttpSession session = request.getSession();
        String azhanghao = (String) session.getAttribute("Azhanghao");
        List<Map<String, Object>> maps = null;
        if ("publisher".equals(select)) {
            maps = informService.adminfindSendZhanghao("'%" + text + "%'", azhanghao);
            System.out.println(maps);
        } else if ("receiver".equals(select)) {
            maps = informService.adminfindReceiverZhanghao(azhanghao, "'%" + text + "%'");
        }
        request.setAttribute("inform", maps);
        request.setAttribute("yeshu", 1);
        return "admin/adminInform";
    }

    @RequestMapping("/add")
    @ResponseBody
    public boolean add(@Param("select") Integer select, @Param("receive") String receive,
                       @Param("textarea") String textarea,HttpServletRequest request) {

        HttpSession session = request.getSession();
        String azhanghao = (String) session.getAttribute("Azhanghao");
        admin.setAzhanghao(azhanghao);
        Admin byZhanghao = adminService.findByZhanghao(admin);

        inform1.setInformTypeId(select);
        inform1.setSendTime(new Date());
        inform1.setAdminId(byZhanghao.getId());
        inform1.setInformation(textarea);

        if (select == 1){
            inform1.setReceiverId(null);
            int i = informService.insertPublic(inform1);
            if(i>0){
                return true;
            }
        }else if(select==2){
            user.setZhanghao(receive);
            User byUser = userService.findByUser(user);
            if(byUser != null){
                inform1.setReceiverId(byUser.getId());
                int i = informService.insertPublic(inform1);
                if(i > 0){
                    return true;
                }
            }
        }
        return false;
    }

    @RequestMapping("/remind")
    @ResponseBody
    public boolean remind(@Param("borrid")Integer borrid,HttpServletRequest request) {
        borrow1.setId(borrid);
        Map<String, Object> byBorrid = borrowService.findByBorrid(borrow1);
        if (byBorrid == null) {
            return false;
        }

        Date endTime = (Date) byBorrid.get("returnTime");
        Date xujieTime = (Date) byBorrid.get("xujieTime");

        if (xujieTime != null) {
            endTime = xujieTime;
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date d = new Date();
        String format1 = simpleDateFormat.format(d);
        String format = simpleDateFormat.format(endTime);
        String[] split1 = format1.split("-");
        String[] split2 = format.split("-");
        LocalDate startDate = LocalDate.of(Integer.parseInt(split1[0]), Integer.parseInt(split1[1]), Integer.parseInt(split1[2]));
        LocalDate startDate1 = LocalDate.of(Integer.parseInt(split2[0]), Integer.parseInt(split2[1]), Integer.parseInt(split2[2]));



        long daysDiff = ChronoUnit.DAYS.between(startDate1, startDate);
        System.out.println("daysDiff:" + daysDiff);
        if (daysDiff > 0) {
            HttpSession session = request.getSession();
            String azhanghao = (String) session.getAttribute("Azhanghao");
            admin.setAzhanghao(azhanghao);
            Admin byZhanghao = adminService.findByZhanghao(admin);

            inform1.setAdminId(byZhanghao.getId());
            inform1.setSendTime(new Date());
            inform1.setInformTypeId(2);
            inform1.setReceiverId((Integer) byBorrid.get("userId"));

            String begin = simpleDateFormat.format((Date) byBorrid.get("borrowTime"));
            String informtion = "用户,你在" + begin + "所借阅的" + byBorrid.get("bName") + "已经超时了，请尽快归还";

            inform1.setInformation(informtion);

            int i = informService.insertPublic(inform1);
            if(i > 0) {
                return true;
            }
        }
        return false;
    }
}

